﻿//  Created by Mathew Purchase.
//  Copyright (c) 2014 Mathew Purchase. All rights reserved.

using UnityEngine;
using System.Collections;
using System.Collections.Generic;

public class terrainGeneration : MonoBehaviour {



	//---------------------------------------------------------------------------------------------
	// Properties
	
	public int terraincount;
	public int terrainmax = 150;
	static public int terrainHeight = 100;
	static public int terrainWidth = 100;
	public float wallTimer;
	public bool wallGenerate = false;
	public bool tunnelGenerate = false;
	public int floorMakers;
	
	
	//---------------------------------------------------------------------------------------------
	// Members
	
	public GameObject m_playerPiece;
	public GameObject m_floorMaker;
	public GameObject m_terrainPiece;
	public GameObject m_wallPiece;
	public GameObject[,] m_terrainPieces = new GameObject[terrainWidth, terrainHeight]; 
	public List<GameObject> m_settlementPieces;
	public bool[,] m_activeTerrain = new bool[terrainWidth , terrainHeight];
	
	
	
	//---------------------------------------------------------------------------------------------
	// Events and Delegates
	
	public delegate void TerrainGenStartupManager();
	public static event TerrainGenStartupManager ready;
	
	//---------------------------------------------------------------------------------------------
	// Init
	void Start () 
	{
		
		terrainGen();
		for (int a = 0; a <= floorMakers; a++)
		{
			Instantiate(m_floorMaker, new Vector3((terrainWidth/2), 0, (terrainHeight/2)), Quaternion.identity);
		}
		
		
	}
	
	//---------------------------------------------------------------------------------------------
	// Core Functions
	
	void Update()
	{
		makeWall();
		
	}

	//---------------------------------------------------------------------------------------------
	void terrainGen()
	{
		for (int x = 0; x < terrainWidth; x++)
		{
			
			for (int y = 0; y < terrainHeight; y++)
			{
				m_activeTerrain[x,y] = false;
			}
			
		}
		
	}
	
	//---------------------------------------------------------------------------------------------
	void makeWall()
	{
		
		
		if (wallGenerate == false)
		{
			wallTimer += 1.0F * Time.deltaTime;
		}
		
		if ((wallTimer > 4f) && (wallGenerate == false))
		{
			if (tunnelGenerate == false)
			{
				Debug.Log ("Tunnels initiated");
				setTunnels();
			}
			for (int x = 0; x < terrainWidth; x++)
			{
				
				for (int y = 0; y < terrainHeight; y++)
				{
					
					if(m_activeTerrain[x,y] == false)
					{	
					
						GameObject piece = Instantiate(m_wallPiece, new Vector3(x, 0, y), Quaternion.identity) as GameObject;
						piece.transform.parent = GameObject.Find ("WallContainer").transform;
						
					}
//					if(m_activeTerrain[x,y] == true)
//					{	
//						if (m_activeTerrain[x+1,y] == false)
//						{
//							GameObject piece = Instantiate(m_wallPiece, new Vector3(x+1, 0, y), Quaternion.identity) as GameObject;
//							piece.transform.parent = GameObject.Find ("WallContainer").transform;
//						}
//						if (m_activeTerrain[x-1,y] == false)
//						{
//							GameObject piece = Instantiate(m_wallPiece, new Vector3(x-1, 0, y), Quaternion.identity) as GameObject;
//							piece.transform.parent = GameObject.Find ("WallContainer").transform;
//						}
//						if (m_activeTerrain[x,y+1] == false)
//						{
//							GameObject piece = Instantiate(m_wallPiece, new Vector3(x, 0, y+1), Quaternion.identity) as GameObject;
//							piece.transform.parent = GameObject.Find ("WallContainer").transform;
//						}
//						if (m_activeTerrain[x,y-1] == false)
//						{
//							GameObject piece = Instantiate(m_wallPiece, new Vector3(x, 0, y-1), Quaternion.identity) as GameObject;
//							piece.transform.parent = GameObject.Find ("WallContainer").transform;
//						}
//					}
				}
				
			}
			if (ready != null)
			{
				ready();
				
			}
			
			
			wallGenerate = true;
			Debug.Log ("Tunnels created");
			m_playerPiece.transform.Translate(new Vector3(50,0,50));
		}
		
	}
	//---------------------------------------------------------------------------------------------
	void setTunnels()
	{		
		tunnelGenerate = true;	
	}
	
	
}

